package com.xu.tools.util;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @author xuguan
 * @date 2023/2/12
 * @desc Spring容器工具类, 通过ApplicationContextAware回调接口实现
 */
@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static ApplicationContext ctx;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        ctx = applicationContext;
    }

    public <T> T getBean(String name, Class<T> clazz) {
        return ctx.getBean(name, clazz);
    }

    public static Object getBean(String name) {
        return ctx.getBean(name);
    }
}
